
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;


/**
 * @author Dancingwithdeath
 */
public class FFMIDlet extends MIDlet implements CommandListener, ItemCommandListener {

    private boolean midletPaused = false;
    
    private FlipFlop gameCanvas;
    private Thread t;
    private Display d;

    //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
    private Command Exit;
    private Command Open;
    private Command BackToMainMenu;
    private Command okCommand;
    private Command StartG;
    private Form MainMenu;
    private ImageItem imageItem;
    private StringItem stringItem;
    private StringItem stringItem2;
    private StringItem stringItem1;
    private Form Howtoplay;
    private StringItem stringItem4;
    private Form About;
    private StringItem stringItem3;
    private Form level1;
    private Image image;
    //</editor-fold>//GEN-END:|fields|0|

    /**
     * The FFMIDlet constructor.
     */
    public FFMIDlet() {
    }

    //<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|
    //</editor-fold>//GEN-END:|methods|0|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize
    /**
     * Initilizes the application.
     * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
     */
    private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize
        // write pre-initialize user code here
//GEN-LINE:|0-initialize|1|0-postInitialize
        // write post-initialize user code here
    }//GEN-BEGIN:|0-initialize|2|
    //</editor-fold>//GEN-END:|0-initialize|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Started point.
     */
    public void startMIDlet() {//GEN-END:|3-startMIDlet|0|3-preAction
        // write pre-action user code here
        switchDisplayable(null, getMainMenu());//GEN-LINE:|3-startMIDlet|1|3-postAction
        // write post-action user code here
    }//GEN-BEGIN:|3-startMIDlet|2|
    //</editor-fold>//GEN-END:|3-startMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
     */
    public void resumeMIDlet() {//GEN-END:|4-resumeMIDlet|0|4-preAction
        // write pre-action user code here
//GEN-LINE:|4-resumeMIDlet|1|4-postAction
        // write post-action user code here
    }//GEN-BEGIN:|4-resumeMIDlet|2|
    //</editor-fold>//GEN-END:|4-resumeMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch
    /**
     * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
     * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
     * @param nextDisplayable the Displayable to be set
     */
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch
        // write pre-switch user code here
        Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }//GEN-END:|5-switchDisplayable|1|5-postSwitch
        // write post-switch user code here
    }//GEN-BEGIN:|5-switchDisplayable|2|
    //</editor-fold>//GEN-END:|5-switchDisplayable|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|7-commandAction|0|7-preCommandAction
    /**
     * Called by a system to indicated that a command has been invoked on a particular displayable.
     * @param command the Command that was invoked
     * @param displayable the Displayable where the command was invoked
     */
    public void commandAction(Command command, Displayable displayable) {//GEN-END:|7-commandAction|0|7-preCommandAction
        // write pre-action user code here
        if (displayable == About) {//GEN-BEGIN:|7-commandAction|1|69-preAction
            if (command == BackToMainMenu) {//GEN-END:|7-commandAction|1|69-preAction
                // write pre-action user code here
                switchDisplayable(null, getMainMenu());//GEN-LINE:|7-commandAction|2|69-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|3|67-preAction
        } else if (displayable == Howtoplay) {
            if (command == BackToMainMenu) {//GEN-END:|7-commandAction|3|67-preAction
                // write pre-action user code here
                switchDisplayable(null, getMainMenu());//GEN-LINE:|7-commandAction|4|67-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|5|65-preAction
        } else if (displayable == MainMenu) {
            if (command == Exit) {//GEN-END:|7-commandAction|5|65-preAction
                // write pre-action user code here
                exitMIDlet();//GEN-LINE:|7-commandAction|6|65-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|7|62-preAction
        } else if (displayable == level1) {
            if (command == BackToMainMenu) {//GEN-END:|7-commandAction|7|62-preAction
                // write pre-action user code here
                switchDisplayable(null, getMainMenu());//GEN-LINE:|7-commandAction|8|62-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|9|7-postCommandAction
        }//GEN-END:|7-commandAction|9|7-postCommandAction
        // write post-action user code here
    }//GEN-BEGIN:|7-commandAction|10|
    //</editor-fold>//GEN-END:|7-commandAction|10|


    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: MainMenu ">//GEN-BEGIN:|14-getter|0|14-preInit
    /**
     * Returns an initiliazed instance of MainMenu component.
     * @return the initialized component instance
     */
    public Form getMainMenu() {
        if (MainMenu == null) {//GEN-END:|14-getter|0|14-preInit
       // Image img = null;
            // write pre-init user code here
            MainMenu = new Form("MainMenu", new Item[] { getImageItem(), getStringItem1(), getStringItem2(), getStringItem() });//GEN-BEGIN:|14-getter|1|14-postInit
            MainMenu.addCommand(getExit());
            MainMenu.setCommandListener(this);//GEN-END:|14-getter|1|14-postInit
            //   img = "pct/mainmenu.jpeg";
           // MainMenu.append("pct/mainmenu.jpeg");
            // write post-init user code here
        }//GEN-BEGIN:|14-getter|2|
        return MainMenu;
    }
    //</editor-fold>//GEN-END:|14-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: imageItem ">//GEN-BEGIN:|26-getter|0|26-preInit
    /**
     * Returns an initiliazed instance of imageItem component.
     * @return the initialized component instance
     */
    public ImageItem getImageItem() {
        if (imageItem == null) {//GEN-END:|26-getter|0|26-preInit
            // write pre-init user code here
            imageItem = new ImageItem("", getImage(), ImageItem.LAYOUT_DEFAULT | ImageItem.LAYOUT_NEWLINE_BEFORE | ImageItem.LAYOUT_NEWLINE_AFTER | Item.LAYOUT_SHRINK | Item.LAYOUT_VSHRINK | Item.LAYOUT_EXPAND | Item.LAYOUT_VEXPAND | Item.LAYOUT_2, "<Missing Image>", Item.PLAIN);//GEN-BEGIN:|26-getter|1|26-postInit
            imageItem.setPreferredSize(400, 250);//GEN-END:|26-getter|1|26-postInit
           // write post-init user code here
        }//GEN-BEGIN:|26-getter|2|
        return imageItem;
    }
    //</editor-fold>//GEN-END:|26-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: image ">//GEN-BEGIN:|27-getter|0|27-preInit
    /**
     * Returns an initiliazed instance of image component.
     * @return the initialized component instance
     */
    public Image getImage() {
        if (image == null) {//GEN-END:|27-getter|0|27-preInit
            // write pre-init user code here
            try {//GEN-BEGIN:|27-getter|1|27-@java.io.IOException
                image = Image.createImage("/pct/mainmenu.jpeg");
            } catch (java.io.IOException e) {//GEN-END:|27-getter|1|27-@java.io.IOException
                e.printStackTrace();
            }//GEN-LINE:|27-getter|2|27-postInit
            // write post-init user code here
        }//GEN-BEGIN:|27-getter|3|
        return image;
    }
    //</editor-fold>//GEN-END:|27-getter|3|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: Howtoplay ">//GEN-BEGIN:|28-getter|0|28-preInit
    /**
     * Returns an initiliazed instance of Howtoplay component.
     * @return the initialized component instance
     */
    public Form getHowtoplay() {
        if (Howtoplay == null) {//GEN-END:|28-getter|0|28-preInit
            // write pre-init user code here
            Howtoplay = new Form("How to Play", new Item[] { getStringItem4() });//GEN-BEGIN:|28-getter|1|28-postInit
            Howtoplay.addCommand(getBackToMainMenu());
            Howtoplay.setCommandListener(this);//GEN-END:|28-getter|1|28-postInit
            // write post-init user code here
        }//GEN-BEGIN:|28-getter|2|
        return Howtoplay;
    }
    //</editor-fold>//GEN-END:|28-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: Open ">//GEN-BEGIN:|30-getter|0|30-preInit
    /**
     * Returns an initiliazed instance of Open component.
     * @return the initialized component instance
     */
    public Command getOpen() {
        if (Open == null) {//GEN-END:|30-getter|0|30-preInit
            // write pre-init user code here
            Open = new Command("Ok", Command.OK, 0);//GEN-LINE:|30-getter|1|30-postInit
            // write post-init user code here
        }//GEN-BEGIN:|30-getter|2|
        return Open;
    }
    //</editor-fold>//GEN-END:|30-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: About ">//GEN-BEGIN:|37-getter|0|37-preInit
    /**
     * Returns an initiliazed instance of About component.
     * @return the initialized component instance
     */
    public Form getAbout() {
        if (About == null) {//GEN-END:|37-getter|0|37-preInit
            // write pre-init user code here
            About = new Form("About", new Item[] { getStringItem3() });//GEN-BEGIN:|37-getter|1|37-postInit
            About.addCommand(getBackToMainMenu());
            About.setCommandListener(this);//GEN-END:|37-getter|1|37-postInit
            // write post-init user code here
        }//GEN-BEGIN:|37-getter|2|
        return About;
    }
    //</editor-fold>//GEN-END:|37-getter|2|


    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItem ">//GEN-BEGIN:|44-getter|0|44-preInit
    /**
     * Returns an initiliazed instance of stringItem component.
     * @return the initialized component instance
     */
    public StringItem getStringItem() {
        if (stringItem == null) {//GEN-END:|44-getter|0|44-preInit
            // write pre-init user code here
            stringItem = new StringItem("", "About", Item.BUTTON);//GEN-BEGIN:|44-getter|1|44-postInit
            stringItem.addCommand(getOpen());
            stringItem.setItemCommandListener(this);
            stringItem.setDefaultCommand(getOpen());
            stringItem.setLayout(ImageItem.LAYOUT_CENTER);//GEN-END:|44-getter|1|44-postInit
            // write post-init user code here
        }//GEN-BEGIN:|44-getter|2|
        return stringItem;
    }
    //</editor-fold>//GEN-END:|44-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Items ">//GEN-BEGIN:|17-itemCommandAction|0|17-preItemCommandAction
    /**
     * Called by a system to indicated that a command has been invoked on a particular item.
     * @param command the Command that was invoked
     * @param displayable the Item where the command was invoked
     */
    public void commandAction(Command command, Item item) {//GEN-END:|17-itemCommandAction|0|17-preItemCommandAction
        // write pre-action user code here
        if (item == stringItem) {//GEN-BEGIN:|17-itemCommandAction|1|49-preAction
            if (command == Open) {//GEN-END:|17-itemCommandAction|1|49-preAction
                // write pre-action user code here
                switchDisplayable(null, getAbout());//GEN-LINE:|17-itemCommandAction|2|49-postAction
                // write post-action user code here
            }//GEN-BEGIN:|17-itemCommandAction|3|95-preAction
        } else if (item == stringItem1) {
            if (command == StartG) {//GEN-END:|17-itemCommandAction|3|95-preAction
                // write pre-action user code here
//GEN-LINE:|17-itemCommandAction|4|95-postAction
               prov();
                // write post-action user code here
            }//GEN-BEGIN:|17-itemCommandAction|5|54-preAction
        } else if (item == stringItem2) {
            if (command == Open) {//GEN-END:|17-itemCommandAction|5|54-preAction
                // write pre-action user code here
                switchDisplayable(null, getHowtoplay());//GEN-LINE:|17-itemCommandAction|6|54-postAction
                // write post-action user code here
            }//GEN-BEGIN:|17-itemCommandAction|7|17-postItemCommandAction
        }//GEN-END:|17-itemCommandAction|7|17-postItemCommandAction
        // write post-action user code here
    }//GEN-BEGIN:|17-itemCommandAction|8|
    //</editor-fold>//GEN-END:|17-itemCommandAction|8|



    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItem1 ">//GEN-BEGIN:|51-getter|0|51-preInit
    /**
     * Returns an initiliazed instance of stringItem1 component.
     * @return the initialized component instance
     */
    public StringItem getStringItem1() {
        if (stringItem1 == null) {//GEN-END:|51-getter|0|51-preInit
            // write pre-init user code here
            stringItem1 = new StringItem("", "Start", Item.BUTTON);//GEN-BEGIN:|51-getter|1|51-postInit
            stringItem1.addCommand(getStartG());
            stringItem1.setItemCommandListener(this);
            stringItem1.setDefaultCommand(getStartG());
            stringItem1.setLayout(ImageItem.LAYOUT_CENTER);//GEN-END:|51-getter|1|51-postInit
            // write post-init user code here
        }//GEN-BEGIN:|51-getter|2|
        return stringItem1;
    }
    //</editor-fold>//GEN-END:|51-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItem2 ">//GEN-BEGIN:|53-getter|0|53-preInit
    /**
     * Returns an initiliazed instance of stringItem2 component.
     * @return the initialized component instance
     */
    public StringItem getStringItem2() {
        if (stringItem2 == null) {//GEN-END:|53-getter|0|53-preInit
            // write pre-init user code here
            stringItem2 = new StringItem("", "How to play", Item.BUTTON);//GEN-BEGIN:|53-getter|1|53-postInit
            stringItem2.addCommand(getOpen());
            stringItem2.setItemCommandListener(this);
            stringItem2.setDefaultCommand(getOpen());
            stringItem2.setLayout(ImageItem.LAYOUT_CENTER);//GEN-END:|53-getter|1|53-postInit
            // write post-init user code here
        }//GEN-BEGIN:|53-getter|2|
        return stringItem2;
    }
    //</editor-fold>//GEN-END:|53-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItem3 ">//GEN-BEGIN:|56-getter|0|56-preInit
    /**
     * Returns an initiliazed instance of stringItem3 component.
     * @return the initialized component instance
     */
    public StringItem getStringItem3() {
        if (stringItem3 == null) {//GEN-END:|56-getter|0|56-preInit
            // write pre-init user code here
            stringItem3 = new StringItem("", "This is standart flip flop game\n\nVersion 1.0.0\n\nAll Right Reserved");//GEN-BEGIN:|56-getter|1|56-postInit
            stringItem3.setLayout(ImageItem.LAYOUT_CENTER | Item.LAYOUT_TOP | Item.LAYOUT_BOTTOM | Item.LAYOUT_VCENTER);//GEN-END:|56-getter|1|56-postInit
            // write post-init user code here
        }//GEN-BEGIN:|56-getter|2|
        return stringItem3;
    }
    //</editor-fold>//GEN-END:|56-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItem4 ">//GEN-BEGIN:|57-getter|0|57-preInit
    /**
     * Returns an initiliazed instance of stringItem4 component.
     * @return the initialized component instance
     */
    public StringItem getStringItem4() {
        if (stringItem4 == null) {//GEN-END:|57-getter|0|57-preInit
            // write pre-init user code here
            stringItem4 = new StringItem("", "\u0418\u0433\u0440\u0430 \u043E\u0447\u0435\u043D\u044C \u043F\u043E\u0445\u043E\u0436\u0430 \u043D\u0430 \u043A\u043B\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043A\u0438\u0439 \u0442\u0435\u0442\u0440\u0438\u0441 \u0438\u043B\u0438 lines ,\u0432 \u043D\u0435\u0439 \u0442\u0430\u043A \u0436\u0435 \u043D\u0443\u0436\u043D\u043E \u0443\u0431\u0440\u0430\u0442\u044C \u043A\u0430\u043A \u043C\u043E\u0436\u043D\u043E \u0431\u043E\u043B\u044C\u0448\u0435\n\u043E\u0434\u0438\u043D\u0430\u043A\u043E\u0432\u044B\u0445 \u043E\u0431\u044A\u0435\u043A\u0442\u043E\u0432. \u041E\u0431\u044A\u0435\u043A\u0442\u044B \u0434\u043E\u043B\u0436\u043D\u044B \u0438\u0441\u0447\u0435\u0437\u0430\u0442\u044C \u043F\u043E \u043C\u0435\u0440\u0435 \u0442\u043E\u0433\u043E \u043A\u0430\u043A \u0442\u0440\u0438 \u043F\u043E\u0434\u0440\u044F\u0434 \u0441\u0442\u043E\u044F\u0449\u0438\u0445 \u043E\u0431\u044A\u0435\u043A\u0442\u0430\n\u0441\u043E\u0432\u043F\u0430\u0434\u0430\u044E\u0442.");//GEN-BEGIN:|57-getter|1|57-postInit
            stringItem4.setLayout(ImageItem.LAYOUT_CENTER | Item.LAYOUT_TOP | Item.LAYOUT_BOTTOM | Item.LAYOUT_VCENTER);//GEN-END:|57-getter|1|57-postInit
            // write post-init user code here
        }//GEN-BEGIN:|57-getter|2|
        return stringItem4;
    }
    //</editor-fold>//GEN-END:|57-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: level1 ">//GEN-BEGIN:|58-getter|0|58-preInit
    /**
     * Returns an initiliazed instance of level1 component.
     * @return the initialized component instance
     */
    public Form getLevel1() {
        if (level1 == null) {//GEN-END:|58-getter|0|58-preInit
            // write pre-init user code here
            level1 = new Form("Level 1");//GEN-BEGIN:|58-getter|1|58-postInit
            level1.addCommand(getBackToMainMenu());
            level1.setCommandListener(this);//GEN-END:|58-getter|1|58-postInit
            // write post-init user code here
        }//GEN-BEGIN:|58-getter|2|
        return level1;
    }
    //</editor-fold>//GEN-END:|58-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: BackToMainMenu ">//GEN-BEGIN:|61-getter|0|61-preInit
    /**
     * Returns an initiliazed instance of BackToMainMenu component.
     * @return the initialized component instance
     */
    public Command getBackToMainMenu() {
        if (BackToMainMenu == null) {//GEN-END:|61-getter|0|61-preInit
            // write pre-init user code here
            BackToMainMenu = new Command("MainMenu", Command.CANCEL, 0);//GEN-LINE:|61-getter|1|61-postInit
            // write post-init user code here
        }//GEN-BEGIN:|61-getter|2|
        return BackToMainMenu;
    }
    //</editor-fold>//GEN-END:|61-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: Exit ">//GEN-BEGIN:|64-getter|0|64-preInit
    /**
     * Returns an initiliazed instance of Exit component.
     * @return the initialized component instance
     */
    public Command getExit() {
        if (Exit == null) {//GEN-END:|64-getter|0|64-preInit
            // write pre-init user code here
            Exit = new Command("Exit", Command.EXIT, 0);//GEN-LINE:|64-getter|1|64-postInit
            // write post-init user code here
        }//GEN-BEGIN:|64-getter|2|
        return Exit;
    }
    //</editor-fold>//GEN-END:|64-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand ">//GEN-BEGIN:|89-getter|0|89-preInit
    /**
     * Returns an initiliazed instance of okCommand component.
     * @return the initialized component instance
     */
    public Command getOkCommand() {
        if (okCommand == null) {//GEN-END:|89-getter|0|89-preInit
            // write pre-init user code here
            okCommand = new Command("123", Command.OK, 0);//GEN-LINE:|89-getter|1|89-postInit
            // write post-init user code here
        }//GEN-BEGIN:|89-getter|2|
        return okCommand;
    }
    //</editor-fold>//GEN-END:|89-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: StartG ">//GEN-BEGIN:|94-getter|0|94-preInit
    /**
     * Returns an initiliazed instance of StartG component.
     * @return the initialized component instance
     */
    public Command getStartG() {
        if (StartG == null) {//GEN-END:|94-getter|0|94-preInit
            // write pre-init user code here
            StartG = new Command("Ok", Command.OK, 0);//GEN-LINE:|94-getter|1|94-postInit
            // write post-init user code here
        }//GEN-BEGIN:|94-getter|2|
        return StartG;
    }
    //</editor-fold>//GEN-END:|94-getter|2|




    /**
     * Returns a display instance.
     * @return the display instance.
     */
    public Display getDisplay () {
        return Display.getDisplay(this);
    }

    /**
     * Exits MIDlet.
     */
    public void exitMIDlet() {
        switchDisplayable (null, null);
        destroyApp(true);
        notifyDestroyed();
    }

    /**
     * Called when MIDlet is started.
     * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
     */
    public void startApp() {
        if (midletPaused) {
            resumeMIDlet ();
        } else {
            initialize ();
            startMIDlet ();
        }
        midletPaused = false;
    }
    
     public void prov() {
         MainMenu.deleteAll();
        this.gameCanvas = new FlipFlop();
        this.t = new Thread(gameCanvas);
        t.start();
        d = Display.getDisplay(this);
        d.setCurrent(gameCanvas);
         
     }

    /**
     * Called when MIDlet is paused.
     */
    public void pauseApp() {
        midletPaused = true;
    }

    /**
     * Called to signal the MIDlet to terminate.
     * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
     */
    public void destroyApp(boolean unconditional) {
    }

}
